package com.kye.express.business.customer_intro;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.business.customer_intro.add.CustomerIntroAddActivity;
import com.kye.express.business.customer_intro.history.CustomerIntroHistoryActivity;
import com.kye.express.business.customer_intro.history.withdraw.CustomerIntroWithDrawHistoryActivity;
import com.kye.express.business.customer_intro.withdraw.CustomerIntroWithdrawActivity;
import com.kye.express.business.web.KyeWebActivity;
import com.kye.express.config.KyeResAction;
import com.kye.express.databinding.ActivityCustomerIntroBinding;
import com.kye.model.bean.customer_intro.GetCashCountResultBean;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import io.reactivex.functions.Consumer;

/**
 * 客户介绍
 */

public class CustomerIntroActivity extends BaseToolbarActivity implements CustomerIntroActivityContract.CustomerIntroActivityView {
    private ActivityCustomerIntroBinding mBinding;
    private CustomerIntroActivityContract.CustomerIntroActivityViewPresenter mPresenter;
    private static final int REQUEST_CODE = 1;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, CustomerIntroActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_customer_intro);
        mPresenter = new CustomerIntroActivityContract.CustomerIntroActivityViewPresenter(this);
        mPresenter.bind(this);
        setUpToolbar((Toolbar) findViewById(R.id.toolbar));
        setTitle(getString(R.string.customer_intro_title));
        initView();
        initEvent();
        mPresenter.getCashCount();
    }

    private void initView() {
        mBinding.refreshLayout.setEnableLoadmore(false);
        mBinding.refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.getCashCount();
            }

            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {

            }

        });
    }

    private void initEvent() {
        //我要体现
        RxView.clicks(mBinding.btnGetCash)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        startActivityForResult(new Intent(CustomerIntroActivity.this, CustomerIntroWithdrawActivity.class), REQUEST_CODE);
                    }
                });
        //我要提现
        RxView.clicks(mBinding.btnGetCashTwo)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        startActivityForResult(new Intent(CustomerIntroActivity.this, CustomerIntroWithdrawActivity.class), REQUEST_CODE);
                    }
                });

        //活动详情
        RxView.clicks(mBinding.tvActivityDetailOne)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        KyeWebActivity.startActivity(CustomerIntroActivity.this, KyeResAction.CUSTOMER_INTRO_ACTIVITY_URL, getString(R.string.customer_intro_activity_detail), false);
                    }
                });
        //活动详情
        RxView.clicks(mBinding.tvActivityDetailTwo)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        KyeWebActivity.startActivity(CustomerIntroActivity.this, KyeResAction.CUSTOMER_INTRO_ACTIVITY_URL, getString(R.string.customer_intro_activity_detail), false);
                    }
                });

        //提现记录
        RxView.clicks(mBinding.tvGetCashRecord)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        CustomerIntroWithDrawHistoryActivity.startActivity(CustomerIntroActivity.this);
                    }
                });
        //介绍记录
        RxView.clicks(mBinding.tvIntroRecord)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        CustomerIntroHistoryActivity.startActivity(CustomerIntroActivity.this);
                    }
                });
        //我要介绍
        RxView.clicks(mBinding.btnIntro)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        CustomerIntroAddActivity.startActivity(CustomerIntroActivity.this,null,true);
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && data != null){
            mPresenter.getCashCount();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.unbind();
        }
    }

    @Override
    public void onComplete() {
        mBinding.refreshLayout.finishRefresh();
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoaded(GetCashCountResultBean getCashCountResultBean) {
        if (getCashCountResultBean != null) {
            if (getCashCountResultBean.getMoney() > 0) {
                mBinding.llCashLayout.setVisibility(View.VISIBLE);
                mBinding.rlViewLayout.setVisibility(View.GONE);
            } else {
                mBinding.rlViewLayout.setVisibility(View.VISIBLE);
                mBinding.llCashLayout.setVisibility(View.GONE);
            }
            mBinding.tvPrice.setText(getCashCountResultBean.getMoney() + "");
        }
    }

    @Override
    public void onOtherError(String msg) {
        mBinding.refreshLayout.finishRefresh();
    }

    @Override
    public void onApiError(String msg) {
        mBinding.refreshLayout.finishRefresh();
    }
}
